from django.shortcuts import render

# Create your views here.

from rest_framework.generics import ListAPIView
from rest_framework.pagination import PageNumberPagination
from rest_framework.filters import OrderingFilter

from lifeHandbook.models import LifeHandbookDirection, LifeHandbookClassification, LifeHandbookArticlesContent
from lifeHandbook.ser import LifeHandbookDirectionSerializer, LifeHandbookClassificationSerializer, LifeHandbookArticlesContentSerializer

# 分页配置
class StandardResultsSetPagination(PageNumberPagination):
    page_size = 10
    page_size_query_param = 'page_size'
    max_page_size = 100

# 排序配置
class CustomOrderingFilter(OrderingFilter):
    ordering_params = ('name', 'manual_Time')

# 生活手册方向 ListAPIView
class LifeHandbookDirectionListView(ListAPIView):
    serializer_class = LifeHandbookDirectionSerializer
    pagination_class = StandardResultsSetPagination
    filter_backends = [CustomOrderingFilter]
    ordering_fields = ['name']

    def get_queryset(self):
        return LifeHandbookDirection.objects.all()

# 生活手册分类 ListAPIView
class LifeHandbookClassificationListView(ListAPIView):
    serializer_class = LifeHandbookClassificationSerializer
    pagination_class = StandardResultsSetPagination
    filter_backends = [CustomOrderingFilter]
    ordering_fields = ['name']

    def get_queryset(self):
        return LifeHandbookClassification.objects.all()

# 生活手册文章 ListAPIView
class LifeHandbookArticlesContentListView(ListAPIView):
    serializer_class = LifeHandbookArticlesContentSerializer
    pagination_class = StandardResultsSetPagination
    filter_backends = [CustomOrderingFilter]
    ordering_fields = ['manual_title']

    def get_queryset(self):
        return LifeHandbookArticlesContent.objects.all()
